#coding=utf-8

import urllib
import urllib2
import httplib
import cookielib

class loginPageGeter:
    
    httpcookie = ''

    #构造函数
    def __init__(self):
        cookie = cookielib.CookieJar()
        self.httpcookie = urllib2.HTTPCookieProcessor(cookie)
    
    #登录函数
    def login(self, url, params):
        try:
            params = urllib.urlencode(params)
            req = urllib2.Request(url, params)
            opener = urllib2.build_opener(self.httpcookie)
            fp = opener.open(req)
            return fp.read()
        except:
            return False
    
    #post数据
    def post(self, url, params):
        try:
            params = urllib.urlencode(params)
            req = urllib2.Request(url, params)
            opener = urllib2.build_opener(self.httpcookie)
            fp = opener.open(req)
            return fp.read()
        except:
            return False

if __name__=='__main__':
    loginObj = loginPageGeter()

    #登录
    print '正在登录...'.decode('utf-8')
    url = 'http://xuxb.v2.local/member/login.php'
    params = {'username':'test01', 'password':'test', 'act':'login'}
    loginObj.login(url, params)
    
    #给用户发10条消息
    for i in range(1, 11):
        print ('%s\t正在发送消息...' % i).decode('utf-8')
        url = 'http://xuxb.v2.local/member/send_message.php'
        params = {'friendId':'1790251', 'message':'机器自动发送', 'act':'add'}
        loginObj.post(url, params)